#!/usr/bin/env ruby
# -*- coding: us-ascii -*-

module Gibberish
  ALPHA = ('a'..'z').to_a
  ALPHASIZE = ALPHA.size
  
  def wordn(num_letters_ave)
    r = rand(num_letters_ave*2) + 1
    letters = []
    r.times { letters << ALPHA[rand(ALPHASIZE)] }
    word = letters.join
    word.capitalize! if rand(8).zero?
    word
  end
  
  def phrasen(num_words_ave)
    r = rand(num_words_ave*2) + 1
    words = []
    r.times { words << wordn(5) }
    words.join(' ')
  end
  
  def sentencen(num_phrases_ave)
    r = rand(num_phrases_ave*2) + 1
    phrases = []
    r.times { phrases << phrasen(3) }
    sentence = phrases.join(', ')
    sentence[0,1] = sentence[0,1].upcase
    sentence + '.'
  end
  
  def paragraphn(num_sentences_ave)
    r = rand(num_sentences_ave*2) + 1
    sents = []
    r.times { sents << sentencen(2) }
    sents.join('  ')
  end
  
  def textn(num_paragraphs_ave)
    r = rand(num_paragraphs_ave*2) + 1
    paras = []
    r.times { paras << paragraphn(4) }
    paras.join($/ * 2)
  end
end

########################################

if $0 == __FILE__
  include Gibberish
  
  require File.join(File.dirname(__FILE__), 'handy4testing')
  include Handy4Testing
  
  ##########
  
  p ALPHA
  
  a = []
  stress(10) do
    word = wordn(5)
    raise 'word too short' unless word.length > 0
    a << word
  end
  
  phrase = a.join(' ')
  puts phrase
  
  ##########
  puts '=========='
  
  puts phrasen(3)
  
  puts sentencen(2)
  
  ##########
  puts '=========='
  
  puts paragraphn(4)
  
  ##########
  puts '=========='
  
  puts textn(5)
end
